package com.kaily.imagelib.element;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;

import com.kaily.imagelib.IMergeImageElement;
import com.kaily.imagelib.utils.ImageUtils;

/**
 * 时间元素
 *
 * @author kaily_zhou
 * @date 2018/10/22 下午2:48
 */
public class DateTimeElement implements IMergeImageElement {
    public static final int COLOR_FONT = 0xFFFFFFFF;
    private boolean isRequireElement;
    private String[] dateTime;
    private float[] fontSizes;
    private int fontColor = -1;
    private int left;
    private int top;
    private Paint yearPaint;
    private Paint monthPaint;
    private Paint dayPaint;
    private String yearText;
    private String monthText;
    private String dayText;

    public DateTimeElement(String[] dateTime, float[] fontSizes, int fontColor, int left, int top, boolean isRequireElement) {
        this.dateTime = dateTime;
        this.fontSizes = fontSizes;
        this.fontColor = fontColor;
        this.left = left;
        this.top = top;
        this.isRequireElement = isRequireElement;
    }

    private void initPaint() {
        fontColor = fontColor < 0 ? COLOR_FONT : fontColor;
        yearPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        yearPaint.setColor(fontColor);

        monthPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        monthPaint.setColor(fontColor);
        monthPaint.setTypeface(Typeface.DEFAULT_BOLD);

        dayPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        dayPaint.setColor(fontColor);
        dayPaint.setTypeface(Typeface.DEFAULT_BOLD);
    }

    @Override
    public boolean generateElement() {
        if (this.dateTime == null || this.dateTime.length != 3) {
            return !isRequireElement;
        }
        if (this.fontSizes == null || this.fontSizes.length != 3) {
            return !isRequireElement;
        }
        try {
            yearText = this.dateTime[0];
            monthText = this.dateTime[1];
            dayText = this.dateTime[2];
            initPaint();

            yearPaint.setTextSize(fontSizes[0]);
            monthPaint.setTextSize(fontSizes[1]);
            dayPaint.setTextSize(fontSizes[2]);
            return true;
        } catch (Exception e) {
            return !isRequireElement;
        }
    }

    @Override
    public boolean mergeImage(Canvas canvas) {
        float[] coordinate = new float[]{left, top};
        try {
            if (yearText != null && yearText.length() > 0) {
                canvas.save();
                int yearWidth = ImageUtils.getTextWidth(yearText, yearPaint);
                int yearHeight = ImageUtils.getTextHeight(yearPaint);
                canvas.translate(coordinate[0], coordinate[1]);
                canvas.translate(yearHeight, 0);
                canvas.rotate(90, 0, 0);
                Paint.FontMetrics fontMetrics = yearPaint.getFontMetrics();
                float y = Math.abs(fontMetrics.ascent);
                canvas.drawText(yearText, 0, y, yearPaint);
                coordinate[1] = coordinate[1] + yearWidth;
                canvas.restore();
            }
            ImageUtils.drawText(monthText, monthPaint, canvas, coordinate, 0);
            ImageUtils.drawText(dayText, dayPaint, canvas, coordinate, 0);
            return true;
        } catch (Exception e) {
            return !isRequireElement;
        }

    }

    @Override
    public boolean isRequireElement() {
        return isRequireElement;
    }
}
